{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 如何创建字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'张三': 100, '李四': 98, '王五': 45}\n",
      "<class 'dict'>\n",
      "{'name': 'jack', 'age': 20}\n",
      "{}\n"
     ]
    }
   ],
   "source": [
    "# 1.使用{}定义字典\n",
    "scores = {'张三': 100, '李四': 98, '王五': 45}\n",
    "print(scores)\n",
    "print(type(scores))\n",
    "# 2.使用内置函数dict()\n",
    "students = dict(name='jack', age=20)\n",
    "print(students)\n",
    "# 创建空字典\n",
    "d = {}\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 字典元素获取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1001\n",
      "1001\n",
      "None\n",
      "99\n"
     ]
    }
   ],
   "source": [
    "# 获取字典中的元素\n",
    "scores = {'张三': 1001, '李四': 98, '王五': 45}\n",
    "\n",
    "# 1.使用[]\n",
    "print(scores['张三'])\n",
    "# print(scores['陈六'])  # KeyError: '陈六'  报错\n",
    "\n",
    "# 2.使用get()方法\n",
    "print(scores.get('张三'))\n",
    "print(scores.get('陈六'))  # None，不报错\n",
    "print(scores.get('麻七', 99))  # 99是查找指定的键不存在的时候给的默认值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. 字典增删改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "{'李四': 98, '王五': 45}\n",
      "{'李四': 98, '王五': 45, '陈六': 98}\n",
      "{'李四': 98, '王五': 45, '陈六': 100}\n"
     ]
    }
   ],
   "source": [
    "# key的判断\n",
    "scores = {'张三': 232323, '李四': 98, '王五': 45}\n",
    "print('张三' in scores)\n",
    "print('张三' not in scores)\n",
    "# 删除指定的key-value对\n",
    "del scores['张三']\n",
    "# scores.clear()  # 清空字典的所有元素\n",
    "print(scores)\n",
    "# 字典元素的新增\n",
    "scores['陈六'] = 98\n",
    "print(scores)\n",
    "# 字典元素的修改\n",
    "scores['陈六'] = 100\n",
    "print(scores)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4. 获取字典视图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['张三', '李四', '王五'])\n",
      "<class 'dict_keys'>\n",
      "['张三', '李四', '王五']\n",
      "dict_values([100, 98, 45])\n",
      "<class 'dict_values'>\n",
      "[100, 98, 45]\n",
      "dict_items([('张三', 100), ('李四', 98), ('王五', 45)])\n",
      "<class 'dict_items'>\n",
      "[('张三', 100), ('李四', 98), ('王五', 45)]\n"
     ]
    }
   ],
   "source": [
    "scores = {'张三': 100, '李四': 98, '王五': 45}\n",
    "# 获取所有的keys\n",
    "keys = scores.keys()\n",
    "print(keys)\n",
    "print(type(keys))\n",
    "print(list(keys))  # 将所有的key组成的视图转成列表\n",
    "# 获取所有的values\n",
    "values = scores.values()\n",
    "print(values)\n",
    "print(type(values))\n",
    "print(list(values))\n",
    "# 获取所有的键值对items\n",
    "items = scores.items()  # () 表示元组\n",
    "print(items)\n",
    "print(type(items))\n",
    "print(list(items))  # 转换之后的列表元素是由元组组成（元组将在下个章节讲解）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5. 字典元素遍历"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张三 100 100\n",
      "李四 98 98\n",
      "王五 45 45\n"
     ]
    }
   ],
   "source": [
    "scores = {'张三': 100, '李四': 98, '王五': 45}\n",
    "# 字典元素的遍历\n",
    "for item in scores:\n",
    "    print(item, scores[item], scores.get(item))  # 输出键keys,values,values"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 6. 字典的特点"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': '李四'}\n",
      "{'name': '张三', 'nikename': '张三'}\n",
      "[10, 100, 20, 30]\n"
     ]
    }
   ],
   "source": [
    "d = {'name': '张三', 'name': '李四'}  # key不允许重复\n",
    "print(d)\n",
    "\n",
    "dd = {'name': '张三', 'nikename': '张三'}  # value可以重复\n",
    "print(dd)\n",
    "\n",
    "# 元素是无序的\n",
    "lst = [10, 20, 30]\n",
    "lst.insert(1, 100)\n",
    "print(lst)\n",
    "\n",
    "# 元素内对象是不可变对象，例如列表不可以\n",
    "# d = {lst: 100}  # TypeError: unhashable type: 'list'\n",
    "\n",
    "# 可根据需要动态伸缩\n",
    "# 会浪费较大内存，使用空间换时间"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 7. 字典生成式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'FRUITS': 96, 'BOOKS': 78, 'OTHERS': 85}\n"
     ]
    }
   ],
   "source": [
    "items = ['Fruits', 'Books', 'Others']\n",
    "prices = [96, 78, 85]\n",
    "d = {items.upper(): prices for items, prices in zip(items, prices)}  # upper变成大写，zip使用短的那个字典进行生成\n",
    "print(d)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.12 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "270px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  },
  "vscode": {
   "interpreter": {
    "hash": "e42634819b8c191a5d07eaf23810ff32516dd8d3875f28ec3e488928fbd3c187"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
